package com.nuonuo.config;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;
import org.redisson.config.ReadMode;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.redisson.config.TransportMode;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @author duan
 * @version 1.0
 * @date 2024/1/25 16:17
 */
@Configuration
@Getter
public class RedissonConfig {


    @Value("${spring.redis.primary.master}")
    private String master;

    @Value("${spring.redis.primary.nodes}")
    private String nodes;

    @Value("${spring.redis.primary.password}")
    private String password;

    @Value("${spring.redis.primary.database}")
    private int database;

    @Value("${spring.redis.primary.minimumIdleSize}")
    private int minimumIdleSize;

    @Value("${spring.redis.primary.pool.max-active}")
    private int poolSize;

    @Value("${spring.redis.primary.connectTimeout}")
    private int connectTimeout;

    @Value("${spring.redis.primary.timeout}")
    private int timeout;

    @Value("${spring.redis.primary.retryAttempts}")
    private int retryAttempts;

    @Value("${spring.redis.primary.retryInterval}")
    private int retryInterval;

    @Value("${spring.redis.primary.pingConnectionInterval}")
    private int pingConnectionInterval;

    private static final String CONSTANT_CHAR_COMMA = ",";

    private static final String CONSTANT_CHAR_REDIS_FORMAT = "redis://%s";

    @Primary
    @Bean(name = "redissonPrimaryConfig")
    public Config buildPrimaryRedissonConfig()
    {
        Config config = new Config();
        // 创建配置
        config.setCodec(StringCodec.INSTANCE);
        config.setTransportMode(TransportMode.NIO);
        config.useSingleServer()
                .setAddress(nodes)
                .setDatabase(database);
        return config;
    }

    @Primary
    @Bean(name = "primaryRedisson")
    public RedissonUtils buildPrimaryRedisson(@Qualifier("redissonPrimaryConfig") Config config){

        return RedissonUtils.getInstance(config);
    }
}
